package database;

/* run with java junit.textui.TestRunner database.TestOrderedLinkedList */

import java.util.Iterator;
import java.util.NoSuchElementException;
import junit.framework.*;


public class TestOrderedLinkedList extends TestCase{


Dictionary <String, Integer> d;

public TestOrderedLinkedList(String name) {
	super(name);
}

protected void setUp(){
// Set up before each test
	d = new OrderedLinkedList<String, Integer> ();
}

protected void tearDown(){
// Tidy up after each test
	d = null;
}

public void testisEmpty(){
	assertTrue ("isEmpty() failed for empty list", d.isEmpty());
}

public void testclear(){
	d.clear();
	assertTrue ("clear() failed for Empty list ", d.isEmpty());
	d.put("Tiddles", 10);
	assertTrue ("clear() failed for one item list ", ! d.isEmpty());

}

public void testsize(){
	assertEquals ("size() failed for empty list", 0, d.size());
	d.put("Tiddles", 10);
	assertEquals ("size() failed for one item list", d.size(), 1);
}

public void testput(){
	d.put("Tiddles", 10);
	assertEquals("put() failed adding to empty list", 1, d.size());
	d.put("Moggy", 15);
	assertEquals("put() failed adding to one item list", 2, d.size());
}

public void testget(){
	d.put("Tiddles", 10);
	assertTrue("get() failed for one item list", d.get("Tiddles") == 10);
}

public void testgetException() {
	try{
		d.get("Tiddles");
		fail("NoSuchElementException should be thrown by get() if item not in list");
	}
	catch (NoSuchElementException e){
		assertTrue(true);
	}
}

public void testremove(){
	d.put("Tiddles", 10);
	d.remove("Tiddles");
	assertEquals("remove() failed with single item list", 0, d.size());
}

public void testListIterator(){
	Iterator <DictionaryEntry <String, Integer>> it = d.iterator();
	assertFalse("Iterator: has.Next() failed with empty list", it.hasNext());
	d.put("Moggy", 15);
	it = d.iterator();
	DictionaryEntry<String, Integer> e = it.next();
	assertTrue ("Iterator: has.Next() failed with one item list" + e.getKey(), e.getKey().equals("Moggy"));
	

}

public static Test suite(){
// Set up the test suite
	TestSuite suite = new TestSuite();
	suite.addTest(new TestOrderedLinkedList("testisEmpty"));
	suite.addTest(new TestOrderedLinkedList("testsize"));
	suite.addTest(new TestOrderedLinkedList("testclear"));
	suite.addTest(new TestOrderedLinkedList("testput"));
	suite.addTest(new TestOrderedLinkedList("testget"));
	suite.addTest(new TestOrderedLinkedList("testremove"));
	suite.addTest(new TestOrderedLinkedList("testgetException"));
	suite.addTest(new TestOrderedLinkedList("testListIterator"));
	return suite;
}
}
